﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
using Microsoft.Data.ConnectionUI;
using System.Data.Common;
using Microsoft.CSharp;
using System.CodeDom;
using KeelKit.Generators;
using EnvDTE;
using Microsoft.VisualStudio.Shell.Interop;
using VSLangProj80;
using VSLangProj;
using VSLangProj2;
using System.Configuration;
 
 
namespace KeelKit
{
    public static class Kit
    {
        /// <summary>
        /// 根据项目中的文件名称获得项目项
        /// </summary>
        /// <param name="pjt">项目</param>
        /// <param name="name">项目项</param>
        /// <returns></returns>
        public static ProjectItem GetProjectItemByName(Project pjt, string name)
        {
            ProjectItem pi = null;
            for (int i = 1; i <= pjt.ProjectItems.Count ; i++)
            {
                if (pjt.ProjectItems.Item(i).Name == name)
                {
                    pi = pjt.ProjectItems.Item(i);
                    break;
                }
            }
            return pi;
        }
        public static void fff()
        {
            Project pjt=GetProjectDAL ();
             VSProject2 p2=ProjectToVSProject(pjt ) as VSProject2 ;
        
         //    IVsProjectDesignerPage d;
         //    Microsoft.VisualStudio.ManagedInterfaces.ProjectDesigner.IVsProjectDesignerPageSite  sdf;
         //SettingsProvider 
           
         
        }

        /// <summary>
        /// 普通pjt 装欢为 vspjt
        /// </summary>
        /// <param name="pjt"></param>
        /// <returns></returns>
        public static VSProject ProjectToVSProject(Project pjt)
        {
            VSProject p2 = null;
            if ((pjt.Kind == PrjKind.prjKindVBProject) |
               (pjt.Kind == PrjKind.prjKindCSharpProject) |
               (pjt.Kind == PrjKind2.prjKindVJSharpProject))
            {
                p2 = ((VSProject)(pjt.Object));
            }
            return p2;
        }
        public static bool AddProjectRefToProject(Project pjt1, Project pjt2)
        {
            bool ok = false;
            if (pjt1 == null  || pjt2 ==null ) return true;
            VSProject p2;
            p2 = ProjectToVSProject(pjt2); 
            if (p2 != null)
            {
                if (p2.References.Find(pjt2.Name ) == null)
                {
                    try
                    {
                        string s = new Uri(typeof(Keel.DB.Common).Assembly.CodeBase).LocalPath;
                        Reference r = p2.References.AddProject(pjt1 );
                        ok = true;
                    }
                    catch (Exception)
                    {


                    }
                }
            }
            return ok;
        }
        public static bool AddKeelRefToProject(Project pjt)
        {
            bool ok = false;
            VSProject p2;
            if (pjt == null) return true;
            p2 = ProjectToVSProject(pjt);
            if (p2 != null)
            {
                if (p2.References.Find(typeof(Keel.IDatabase).Namespace) == null)
                {
                    try
                    {
                        string s = new Uri(typeof(Keel.DB.Common).Assembly.CodeBase).LocalPath;
                        Reference r = p2.References.Add(s);
                        ok = true;
                    }
                    catch (Exception)
                    {


                    }

                }

            }

            return ok;

        }
        /// <summary>
        /// 根据项目全名的扩展名语言类型
        /// </summary>
        /// <param name="pjt">项目名称</param>
        /// <returns>返回项目类型</returns>
        public static cfLangType GetProjectLangType(EnvDTE.Project pjt)
        {
            string ext = (new FileInfo(pjt.FullName)).Extension.ToLower();
            cfLangType cl = cfLangType.CSharp;
            switch (ext)
            {
                case ".vbproj":
                    cl = cfLangType.VBDotNet;
                    break;
                case ".csproj":
                default:
                    cl = cfLangType.CSharp;
                    break;
            }
            return cl;
        }
        public static string GetProjectLaneExt(cfLangType lt)
        {
            string i = "";
            switch (lt )
            {
                case cfLangType.CSharp:
                    i = ".cs";
                    break;
                case cfLangType.VBDotNet:
                    i = ".vb";
                    break;
                default:
                    i = "cs";
                    break;
            }
            return i;
        }
        public static string GetProjectLangStr(cfLangType lt)
        {
            string i = "";
            switch (lt)
            {
                case cfLangType.CSharp:
                    i = "CSharp";
                    break;
                case cfLangType.VBDotNet:
                    i = "VisualBasic";
                    break;
                default:
                    i = "CSharp";
                    break;
            }
            return i;
        }

        public static bool CheckDataBase()
        {
            bool i = false;
            try
            {
                Keel.DB.Common.NowDataBase = Keel.DB.Common.GetIDatabaseByNamespace(Kit.SlnKeel.ProviderName, Kit.SlnKeel.ConnectString);
                i = true;
            }
            catch (Exception)
            {
                throw new Keel.DB.DataBaseNotConfigException();
            }
            return i;
        }
       
        public static EnvDTE.vsCommandStatus GetCommandStatusForModel()
        {
            EnvDTE.vsCommandStatus cs = EnvDTE.vsCommandStatus.vsCommandStatusUnsupported;
            for (int i = 1; i <= Common.chDTE.Solution.Projects.Count; i++)
            {
                if (Common.chDTE.Solution.Projects.Item(i).Name == Kit.SlnKeel.ModelProjectName)
                {
                    cs = EnvDTE.vsCommandStatus.vsCommandStatusSupported | EnvDTE.vsCommandStatus.vsCommandStatusEnabled  ;
                    break;
                }
            }
            return cs;
        }
  
        public static EnvDTE.Project GetProjectModel()
        {
            return GetProjectByName(Kit.SlnKeel.ModelProjectName);
        }
        public static EnvDTE.Project GetProjectDAL()
        {
            return GetProjectByName(Kit.SlnKeel.DALProjectName );
        }
        public static EnvDTE.Project GetProjectComponent()
        {
            return GetProjectByName(Kit.SlnKeel.ComponentProjectName );
        }
        public static EnvDTE.Project   GetProjectByName(string name)
        {
            EnvDTE.Project pj= null;
            for (int i = 1; i <= Common.chDTE.Solution.Projects.Count; i++)
            {
                if (Common.chDTE.Solution.Projects.Item(i).Name == name )
                {
                    pj = Common.chDTE.Solution.Projects.Item(i);
                    break;
                }
            }
            return pj ;
        }

 

        public static DataSource GetDataSourceByName(string name)
        {
            DataSource ds = null;
            switch (name)
            {
                case "MicrosoftAccess":
                    ds = DataSource.AccessDataSource;
                    break;
                case "OdbcDsn":
                    ds = DataSource.OdbcDataSource;
                    break;
                case "Oracle":
                    ds = DataSource.OracleDataSource;
                    break;
                default:
                case "MicrosoftSqlServer":
                    ds = DataSource.SqlDataSource;
                    break;
                case "MicrosoftSqlServerFile":
                    ds = DataSource.SqlFileDataSource;
                    break;

            }
            return ds;

        }
  
        public static DataProvider GetDataProviderByName(string name)
        {
            DataProvider dp = null;
            switch (name)
            {
                default:
                case "System.Data.SqlClient":
                    dp = DataProvider.SqlDataProvider;
                    break;
                case "System.Data.OracleClient":
                    dp = DataProvider.OracleDataProvider;
                    break;
                case "System.Data.OleDb":
                    dp = DataProvider.OleDBDataProvider;
                    break;
                case "System.Data.Odbc":
                    dp = DataProvider.OdbcDataProvider;
                    break;

            }
            return dp;

        }
        public static DbProviderFactory GetProviderFactoryByName(string name)
        {
            DbProviderFactory dp = null;
            switch (name)
            {
                default:
                case "System.Data.SqlClient":
                    dp =  System.Data.SqlClient.SqlClientFactory.Instance  ;
                    break;
                case "System.Data.OracleClient":
                    dp = System.Data.OracleClient.OracleClientFactory.Instance ;
                    break;
                case "System.Data.OleDb":
                    dp = System.Data.OleDb.OleDbFactory.Instance ;
                    break;
                case "System.Data.Odbc":
                    dp = System.Data.Odbc.OdbcFactory.Instance ;
                    break;

            }
            return dp;

        }
        /// <summary>
        /// 取得Keel配置文件的文件名
        /// </summary>
        /// <returns></returns>
        public static string GetKeelFileName()
        {
            if (Common.chDTE == null) return null;
            FileInfo fi = new FileInfo(Common.chDTE.Solution.FileName);
            if (fi.Exists == false) return null;
            string ex = fi.FullName.Remove(fi.FullName.Length - 3) + "keel";
            return ex;
        }
        private static string _slnname = null;
        private static KeelExt ke = null;
        /// <summary>
        /// 设置或者取得当前解决方案的Keel配置信息
        /// </summary>
        public static KeelExt SlnKeel
        {
            get
            {
                  string file = GetKeelFileName();
                if (  file ==_slnname    && ke != null)
                {
                    return ke;
                }
                _slnname = file;

                if (File.Exists(file))
                {
                    try
                    {
                        XmlSerializer xs = new XmlSerializer(typeof(KeelExt));
                        XmlReader xr = XmlReader.Create(new MemoryStream(File.ReadAllBytes(GetKeelFileName())));
                        if (xs.CanDeserialize(xr))
                        {
                            ke = xs.Deserialize(xr) as KeelExt;
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
                else
                {
                    ke = new KeelExt();
                }
                return ke;
            }
            set
            {
                ke = value;
                XmlSerializer xs = new XmlSerializer(typeof(KeelExt));
                MemoryStream ms = new MemoryStream();
                xs.Serialize(ms, ke);
                System.IO.File.WriteAllBytes(GetKeelFileName(), ms.ToArray());
            }
        }

        public static  string GetKeelKitPath()
        {
            return  new Uri(typeof(KeelExt).Assembly.CodeBase).LocalPath;
        }
    }
}
